package com.goldze.mvvmhabit.ui.selectsdepartment;

import android.databinding.ObservableField;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;

import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.entity.response.SelectDepartmentResponseEntity;
import com.goldze.mvvmhabit.utils.UserManager;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;


public class SelectDepartmentItemViewModel extends ItemViewModel<SelectDepartmentViewModel> {

    public ObservableField<SelectDepartmentResponseEntity> entity = new ObservableField<>();

    public Drawable drawableWhite, drawableBlue;
    public int whiteColor, blackColor;

    public SelectDepartmentItemViewModel(@NonNull SelectDepartmentViewModel viewModel, SelectDepartmentResponseEntity entity) {
        super(viewModel);
        this.entity.set(entity);
        drawableBlue = ContextCompat.getDrawable(viewModel.getApplication(), R.drawable.department_btn_blue_shape);
        drawableWhite = ContextCompat.getDrawable(viewModel.getApplication(), R.drawable.department_btn_white_shape);

        whiteColor = ContextCompat.getColor(viewModel.getApplication(), R.color.white);
        blackColor = ContextCompat.getColor(viewModel.getApplication(), R.color.color_333333);
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (viewModel.oldPosition != getPosition()) {
                for (int j = 0; j < viewModel.observableList.size(); j++) {
                    if (viewModel.observableList.get(j).entity.get().isIsdelete()) {
                        viewModel.observableList.get(j).entity.get().setIsdelete(false);
//                        int index = viewModel.observableList.indexOf(viewModel.observableList.get(j));
                        viewModel.upDataUi(j, viewModel.observableList.get(j));
                    }
                }
            }
            if (entity.get().isIsdelete()) {
                entity.get().setIsdelete(false);
            } else {
                entity.get().setIsdelete(true);
            }
            viewModel.oldPosition = getPosition();
            UserManager.getAppManager().setmSelectDepartment(entity.get());
            viewModel.upDataUi(getPosition(), viewModel.observableList.get(getPosition()));
        }
    });

}
